package com.itany.boot.controller;

import com.github.pagehelper.PageInfo;
import com.itany.boot.constant.ResponseCodeConstant;
import com.itany.boot.entity.Course;
import com.itany.boot.entity.CourseType;
import com.itany.boot.exception.CourseExistException;
import com.itany.boot.exception.FileUploadErrorException;
import com.itany.boot.exception.RequestParameterErrorException;
import com.itany.boot.service.CourseService;
import com.itany.boot.service.CourseTypeService;
import com.itany.boot.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date 2025/10/12
 */
@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    CourseTypeService courseTypeService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(Course courseParam, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        List<CourseType> enable = courseTypeService.findEnabled();
        mav.addObject("enable", enable);
        List<CourseType> type = courseTypeService.findAll();
        mav.addObject("type", type);

        PageInfo<Course> coursePage = courseService.findPage(courseParam, pageNo, pageSize);
        mav.addObject("coursePage", coursePage);
        mav.addObject("courseParam",courseParam);

        mav.setViewName("back_courseSet");
        return mav;
    }

    @RequestMapping("/addCourse")
    @ResponseBody
    public ResponseResult addCourse(String courseName, String courseInfo, String author, @RequestParam("file") CommonsMultipartFile file, String recommendationGrade, String courseTypeId, HttpSession session) {
        ResponseResult result = new ResponseResult();

        try {
            courseService.addCourse(courseName,courseInfo,author,file,recommendationGrade,courseTypeId,session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (CourseExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (FileUploadErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public ResponseResult findById(String id) {
        ResponseResult result = new ResponseResult();

        try {
            Course course = courseService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(course);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/modifyCourse")
    @ResponseBody
    public ResponseResult modifyCourse(String id, String courseName, String courseInfo, String author, CommonsMultipartFile file, String recommendationGrade, String courseTypeId, HttpSession session) {
        ResponseResult result = new ResponseResult();

        try {
            courseService.modifyCourse(id,courseName,courseInfo,author,file,recommendationGrade,courseTypeId,session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (CourseExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/modifyStatus")
    @ResponseBody
    public ResponseResult modifyStatus(String id, String status) {
        ResponseResult result = new ResponseResult();

        try {
            courseService.modifyStatus(id,status);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
}
